﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Windows.Navigation;
using BudgetManager.Domain;


namespace BudgetManager.UserControls
{
    public partial class UserControlEditValue : UserControl
    {
       Category _category;
       AbstractValue _abstractValue;
       public UserControlEditValue()
        {
            InitializeComponent();
            
           if(isCreateMode()){
                _category = Category.ReadDefaultCategory();
                 txtCategory.Text = _category.Name;
                datePicker.Value = DateTime.Now;
           }
          
           if (isEditMode())
           {
               _abstractValue = (AbstractValue)PhoneApplicationService.Current.State["abstractvalue"];
               _category = _abstractValue.Category;
               //txtDescription.Text = _abstractValue.Description;
               txtCategory.Text = _category.Name;
               datePicker.Value = _abstractValue.FullDate;
           }

            Object category;
            if (PhoneApplicationService.Current.State.TryGetValue("Category", out category) && category!=null)
            {
                _category = (Category)category; // save state
                txtCategory.Text = _category.Name; //change text box
                PhoneApplicationService.Current.State["Category"] = null; // Clean memory
            }
        }
       public AbstractValue SelectedValue
       {
           get { return _abstractValue; }
       }
        private void txtMoney_TextChanged(object sender, TextChangedEventArgs e)
        {
             
            
        }

        private bool isEditMode()
        {
            object abstractValue;
            return PhoneApplicationService.Current.State.TryGetValue("abstractvalue", out abstractValue) && abstractValue != null; 
        }
        private bool isCreateMode()
        {
            return !isEditMode();
        }

        private  void onTapCategories(object sender, Microsoft.Phone.Controls.GestureEventArgs e)
        {
          // NavigationService.Navigate(new Uri(string.Format("/ManagementCategory.xaml?"), UriKind.Relative));
        
        }

        private void onTapDescription(object sender, System.Windows.Input.GestureEventArgs e)
        {
            txtDescription.Text = String.Empty;
        }
        
    }
}
